package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.PageParam;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Service
public class EmployeeServiceImpl implements EmployeeService {


    @Autowired
    private EmployeeMapper mapper;

    /**
     * 更新员工
     *
     * @param employee
     * @param employeeId
     * @return
     */
    @Override
    public int update(Employee employee, long employeeId) {
        return mapper.updateById(employee);
    }

    /**
     * 分页查询
     *
     * @param pageParam
     * @return
     */
    @Override
    public IPage<Employee> findPage(PageParam pageParam) {
        IPage<Employee> page=new Page<>(pageParam.getPage(), pageParam.getPageSize());
        LambdaQueryWrapper<Employee>  lqw=new LambdaQueryWrapper<>();
        lqw.eq(pageParam.getName()!=null,Employee::getName,pageParam.getName());
        IPage<Employee> iPage = mapper.selectPage(page, lqw);
        System.out.println(iPage);
        return iPage;
    }

    /**
     * 添加员工
     *
     * @param employeeId
     * @return
     */
    @Override
    public int add(Employee employee, long employeeId) {

        //设置初始密码123456
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        return mapper.insert(employee);
    }


}
